home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / external / sharelib / simple.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-08  |  5.2 KB  |  195 lines

  1.   /*
  2. **    $Id: simple.c,v 1.1 1993/11/16 23:36:16 idl Exp $
  3. **
  4. ** NAME:
  5. **
  6. **     simple
  7. **
  8. ** PURPOSE:
  9. **    This C function is used to demonstrate how to pass simple 
  10. **    variables from IDL to a C function using the IDL function
  11. **    CALL_EXTERNAL. The variables values are squared and 
  12. **    returned to show how variable values can be changed.
  13. **
  14. ** CATEGORY:
  15. **    Dynamic Link
  16. **
  17. ** CALLING SEQUENCE:
  18. **    This function is called in IDL by using the following command
  19. **
  20. **    IDL> result = CALL_EXTERNAL('simple.so', 'simple',    $
  21. **    IDL>            byte_var, short_var, long_var, float_var,        $
  22. **    IDL>        double_var, string_var, /S_VALUE ) 
  23. **
  24. ** INPUTS:
  25. **
  26. **    Byte_var:    A scalar byte variable
  27. **
  28. **    Short_var:    A scalar short integer variable
  29. **
  30. **    Long_var:    A scalar long integer variable
  31. **
  32. **    Float_var:    A scalar float variable
  33. **
  34. **    Double_var:    A scalar float variable
  35. **
  36. **    String_var:    A scalar string value (Actally an IDL STRING struct)
  37. **
  38. ** OUTPUTS:
  39. **    The function returns the passed in string value "Squared" and 
  40. **    each numeric variables are squared. 
  41. **
  42. ** SIDE EFFECTS:
  43. **    None.
  44. **
  45. ** RESTRICTIONS:
  46. **    When dealing with IDL Strings, the user should not directly 
  47. **    manipulate the IDL string, but make a copy of the string, 
  48. **    manipulate the copy and , return the new string as a result of
  49. **    the function.
  50. **
  51. ** EXAMPLE:
  52. **-----------------------------------------------------------------------------
  53. ;; The following is the commands that would be used to call this
  54. ;; routine in IDL
  55. ;;
  56.     byte_var     = 1b
  57.     short_var    = 2
  58.     long_var    = 3l
  59.     float_var    = 4.0
  60.     double_var    = 5d0
  61.     string_var    = "SIX"
  62.  
  63.     result = CALL_EXTERNAL('simple.so', 'simple',         $
  64.             byte_var, short_var, long_var, float_var,    $
  65.             double_var, string_var, /S_VALUE )    
  66.  
  67. **-----------------------------------------------------------------------------
  68. **
  69. ** MODIFICATION HISTORY:
  70. **    Written October, 1993        KDB
  71. **
  72. ** Declare header file
  73. */
  74.  
  75. #include <stdio.h>
  76. #include <string.h>
  77.  
  78. /*
  79. ** Declare the structure for an IDL string (From IDL User's Guide).
  80. */
  81.  
  82. typedef struct {
  83.    unsigned short slen;       /* length of the string     */
  84.    short stype;            /* Type of string         */    
  85.    char *s;            /* Pointer to chararcter array     */
  86. } STRING;
  87.  
  88. /*
  89. ** Declare the function
  90. */
  91.  
  92. char *
  93. simple(argc, argv)
  94. int argc;
  95. void *argv[];
  96. {
  97. /*
  98. ** Declare variable 
  99. */
  100.    char     *byte_var;    /* Pointer to a char ( One byte )    */
  101.    short    *short_var;     /* Pointer to short integer        */
  102.    long        *long_var;    /* Pointer to long            */
  103.    float    *float_var;    /* Pointer to float            */
  104.    double    *double_var;    /* Pointer to double            */
  105.    STRING    *string_var;    /* Pointer to IDL string structure     */
  106.    char        *string2;    /* Will hold the value of the string^2    */
  107.  
  108. /*
  109. ** Insure that the correct number of arguments were passed in (argc = 6)
  110. */
  111.    if(argc != 6)
  112.    {
  113.    /*
  114.    ** Print an error message and return
  115.    */
  116.       fprintf(stderr,"simple: Incorrect number of arguments\r\n");
  117.       return((char*)NULL);  /* Signal an error */
  118.    }
  119. /*
  120. ** Cast the pointer in argv to the pointer variables
  121. */
  122.    byte_var    = (char *)   argv[0];
  123.    short_var    = (short *)  argv[1];
  124.    long_var    = (long *)   argv[2];    
  125.    float_var    = (float *)  argv[3];
  126.    double_var    = (double *) argv[4];
  127.    string_var    = (STRING *) argv[5];
  128. /*
  129. ** Now print a header indicating that we are in the called C function
  130. ** The "\r" was added to insure correct operation on Solaris systems
  131. */   
  132.    fprintf(stdout,
  133.     "\r\n-----------------------------------------------------\r\n");
  134.    fprintf(stdout,"Inside C function simple ");
  135.    fprintf(stdout,"(Called from IDL using CALL_EXTERNAL)\r\n\r\n");
  136.  
  137. /*
  138. ** Now print the values of each variable that was passed in from IDL
  139. */
  140.    fprintf(stdout, "Scalar Values Passed in From IDL:\r\n");
  141.    fprintf(stdout, "\tBYTE Parameter: \t%d\r\n", (short)(*byte_var));
  142.    fprintf(stdout, "\tSHORT Parameter: \t%d\r\n", *short_var);
  143.    fprintf(stdout, "\tLONG Parameter: \t%d\r\n", *long_var);
  144.    fprintf(stdout, "\tFLOAT Parameter: \t%f\r\n", *float_var);
  145.    fprintf(stdout, "\tDOUBLE Parameter: \t%f\r\n", *double_var);
  146.  
  147. /*
  148. ** To print the string, de-reference the character string that is part
  149. ** of the IDL string struct.
  150. */
  151.    fprintf(stdout, "\tSTRING Parameter: \t%s\r\n", string_var->s );
  152. /*
  153. ** Now print a line to seperate the output from this function and others
  154. */
  155.    fprintf(stdout,
  156.     "\r\n-----------------------------------------------------\r\n");
  157. /*     
  158. ** Square each variable.
  159. */
  160.    *byte_var     = *byte_var   * (*byte_var);
  161.    *short_var    = *short_var  * (*short_var);
  162.    *long_var    = *long_var   * (*long_var);
  163.    *float_var    = *float_var  * (*float_var);
  164.    *double_var    = *double_var * (*double_var);
  165.  
  166. /*
  167. ** To square the string, just create a string that contains two copies
  168. ** of the IDL string value. Allocate memory for the string and a NULL.
  169. */
  170.    if( (string2 = (char*)malloc(2*(string_var->slen)+1) )
  171.     == (char *)NULL)
  172.    {
  173.    /*
  174.    ** Had a malloc error. write an error message and return a NULL
  175.    */
  176.       fprintf(stderr,"simple: malloc error.\r\n");
  177.       return((char*)NULL);
  178.    }
  179.  
  180. /*
  181. ** Copy the IDL string to the just malloced string variable
  182. */
  183.     string2 = strcpy(string2, string_var->s);
  184.  
  185. /*
  186. ** Now add another copy of the IDL string
  187. */
  188.    string2 = strcat(string2, string_var->s);
  189.  
  190. /*
  191. ** now return the value of string2
  192. */
  193.    return(string2);
  194. }
  195.